﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using personalplaner.common.mvvm;
using System.Windows;
using personalplaner.gui.common;
using System.Windows.Input;
using personalplaner.common.mvvm.commands;
using personalplaner.business.stamm;
using personalplaner.servicemodel.dataaccess;
using System.Collections.ObjectModel;
using personalplaner.gui.Properties;
using personalplaner.common.mvvm.common;
using personalplaner.business;

namespace personalplaner.gui.stamm.viewmodels
{
	public class HolidayStammViewModel : BaseViewModel
	{
		public HolidayStammViewModel(DependencyObject parent)
            : base(parent)
        {
        }

		public override void Loaded()
		{
			var mw = GetService<IMainWindowDisplayContainer>();
			mw.IsWaitVisible = false;
		}

		private void RefreshHolidayDates()
		{
			if (SelectedPerson == null || SelectedYear == null)
				return;

			var da = GetService<IPersonDataAccess>();
			var lst = da.GetHolidays(SelectedPerson.PrsID, SelectedYear.Jahr);

			if (Holidays == null)
			{
				Holidays = lst != null ? new ObservableCollection<Holiday>(lst) : new ObservableCollection<Holiday>();
			}
			else
			{
				Holidays.Clear();
				if (lst == null)
					return;
				foreach (var itm in lst)
					Holidays.Add(itm);
			}

		}

		#region Commands

		ICommand _okcommand;
		public ICommand OkCommand
		{
			get
			{
				if (_okcommand == null)
					_okcommand = new RelayCommand(OnOkCommand);
				return _okcommand;
			}
		}

		private void OnOkCommand(object parameter)
		{
			var wnd = Parent as Window;
			if (wnd != null)
			{
				wnd.DialogResult = true;
				wnd.Close();
				return;
			}

			var mw = GetService<IMainWindowDisplayContainer>();
			mw.MoveToPreviousView();
		}

		ICommand _cancelcommand;
		public ICommand CancelCommand
		{
			get
			{
				if (_cancelcommand == null)
					_cancelcommand = new RelayCommand(OnCancelCommand);
				return _cancelcommand;
			}
		}

		private void OnCancelCommand(object parameter)
		{
			var wnd = Parent as Window;
			if (wnd != null)
			{
				wnd.DialogResult = false;
				wnd.Close();
				return;
			}

			var mw = GetService<IMainWindowDisplayContainer>();
			mw.MoveToPreviousView();
		}

		ICommand _savecommand;
		public ICommand SaveCommand
		{
			get
			{
				if (_savecommand == null)
					_savecommand = new RelayCommand(OnSave, parameter => SelectedPerson != null && SelectedYear != null && SelectedHoliday != null && SelectedHoliday.IsNew);
				return _savecommand;
			}
		}

		private void OnSave(object parameter)
		{
			if (SelectedPerson == null || SelectedYear == null || SelectedHoliday == null )
				return;

			var da = GetService<IPersonDataAccess>();			
			var ferienid = da.SetHoliday(SelectedHoliday);

			// set the id and name so that IsNew = false and add it to the collection
			SelectedHoliday.ID = ferienid;
			SelectedHoliday.Name = "Holiday";

			if (Holidays == null)
				Holidays = new ObservableCollection<Holiday>();

			Holidays.Add(SelectedHoliday);

			OnNew(null);
		}

		ICommand _newcommand;
		public ICommand NewCommand
		{
			get
			{
				if (_newcommand == null)
					_newcommand = new RelayCommand(OnNew, p => SelectedPerson != null && SelectedYear != null);
				return _newcommand;
			}
		}

		private void OnNew(object parameter)
		{
			if (SelectedPerson == null || SelectedYear == null)
				return;


			SelectedHoliday = new Holiday()
			{
				Von = DateTime.Now,
				Bis = DateTime.Now,
				PrsId = SelectedPerson.PrsID
			};

			RaiseMoveFocus("Von");
		}

		ICommand _deleteCommand;
		public ICommand DeleteCommand
		{
			get
			{
				if (_deleteCommand == null)
					_deleteCommand = new RelayCommand(OnDelete, param => SelectedHoliday != null && !SelectedHoliday.IsNew);
				return _deleteCommand;
			}
		}

		private void OnDelete(object parameter)
		{
			if (MessageBox.Show(Resources.SindSicherEintragLoeschen, Resources.EintragLoeschen, MessageBoxButton.YesNo, MessageBoxImage.Exclamation) != MessageBoxResult.Yes)
				return;

			var da = GetService<IPersonDataAccess>();
			da.DeleteHoliday(SelectedHoliday.FerienId);

			Holidays.Remove(SelectedHoliday);

			SelectedHoliday = null;

			//Mediator.NotifyColleagues<SaldoAnpassung>("RefreshPersonSaldo", SelectedSaldoAnpassug);
		}

		#endregion

		#region Properties

		IList<Person> _personen;
		public IList<Person> Personen
		{
			get
			{
				if (Designer.IsDesignMode)
					return new ObservableCollection<Person>();

				if (_personen == null)
				{
                    //get the data access via the service locator.
                    var da = GetService<IPersonDataAccess>();
                    var lst = da.GetPersonen(showAll: ShowAllPersonen);
                    _personen = lst != null ? new ObservableCollection<Person>(lst) : new ObservableCollection<Person>();
				}
				return _personen;
            }
            set
            {
                _personen = value;
                RaisePropertyChanged("Personen");
            }
		}

        public void SetPersonen(int prsId)
        {
            //get the data access via the service locator.
            var da = GetService<IPersonDataAccess>();
            var lst = da.GetPersonen(showAll: ShowAllPersonen, prsId: prsId);
            Personen = lst != null ? new ObservableCollection<Person>(lst) : new ObservableCollection<Person>();
            if (Personen.Count > 0)
                SelectedPerson = Personen[0];
        }

		Person _selectedPerson;
		public Person SelectedPerson
		{
			get { return _selectedPerson; }
			set
			{
				_selectedPerson = value;
				RaisePropertyChanged("SelectedPerson");

				RefreshHolidayDates();
			}
		}

		bool _showAllPersonen;
		public bool ShowAllPersonen
		{
			get
			{
				return _showAllPersonen;
			}
			set
			{
				_showAllPersonen = value;

				_personen = null;
				RaisePropertyChanged("ShowAllPersonen");
				RaisePropertyChanged("Personen");
			}
		}

		IList<PlanYear> _years;
		public IList<PlanYear> Years
		{
			get
			{
				if (_years == null)
				{
					var da = GetService<ICommonDataAccess>();
					var lst = da.GetActiveYears();
					_years = lst != null ? new ObservableCollection<PlanYear>(lst) : new ObservableCollection<PlanYear>();

					if (_years != null)
					{
						SelectedYear = (from y in _years
						                        where y.Status == business.enums.enStatus.Active
						                        select y).FirstOrDefault();

						if (SelectedYear == null && _years.Count > 0)
							SelectedYear = _years[0];
					}
				}
				return _years;
			}
		}

		PlanYear _selectedYear;
		public PlanYear SelectedYear
		{
			get
			{
				return _selectedYear;
			}
			set
			{
				_selectedYear = value;
				RaisePropertyChanged("SelectedYear");

				RefreshHolidayDates();
			}
		}

		IList<Holiday> _holidays;
		public IList<Holiday> Holidays
		{
			get
			{
				return _holidays;
			}
			set
			{
				_holidays = value;
				RaisePropertyChanged("Holidays");
			}
		}

		Holiday _selectedHoliday;
		public Holiday SelectedHoliday
		{
			get { return _selectedHoliday; }
			set
			{
				_selectedHoliday = value;
				RaisePropertyChanged("SelectedHoliday");
				RaisePropertyChanged("IsWriteable");
			}
		}

		public bool IsWriteable
		{
			get
			{
				return SelectedHoliday != null && SelectedHoliday.IsNew;
			}
		}

		#endregion
	}
}
